package bb.lanxing.lib.devices.antplus;

import android.content.Context;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import com.dsi.ant.plugins.antplus.pcc.MultiDeviceSearch;
import com.dsi.ant.plugins.antplus.pcc.defines.DeviceType;
import com.dsi.ant.plugins.antplus.pcc.defines.RequestAccessResult;
//import com.dsi.ant.plugins.antplus.pccbase.MultiDeviceSearch;
import bb.lanxing.lib.devices.api.DeviceProvider;
import bb.lanxing.lib.devices.api.SmartDevice;
import bb.lanxing.lib.devices.core.scanner.AbstractDeviceScanner;
import bb.lanxing.lib.devices.core.scanner.DeviceScanner;
import bb.lanxing.lib.devices.utils.AntPlusUtils;
import java.util.EnumSet;

public class AntPlusScanner extends AbstractDeviceScanner<SmartDevice> {
    private ANTPlusScanAdapter antPlusDeviceCallback;
    private Handler antPlusHandler;
    private MultiDeviceSearch antPlusScanner;
    private DeviceProvider deviceProvider;
    private int deviceType;

    public AntPlusScanner(Context context, DeviceScanner.ScannerCallback<SmartDevice> scannerCallback, DeviceProvider deviceProvider) {
        this(context, scannerCallback, deviceProvider, -1);
    }

    public AntPlusScanner(Context context, DeviceScanner.ScannerCallback<SmartDevice> scannerCallback, DeviceProvider deviceProvider, int i) {
        super(context, scannerCallback);
        this.deviceType = i;
        this.deviceProvider = deviceProvider;
    }

    @Override
    protected boolean doStartScan() {
        return startScanWithAntPlus();
    }

    @Override
    public boolean stopScan() {
        Handler handler;
        final MultiDeviceSearch multiDeviceSearch = this.antPlusScanner;
        if (multiDeviceSearch != null && (handler = this.antPlusHandler) != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    multiDeviceSearch.close();
                }
            });
        }
        return super.stopScan();
    }

    private boolean startScanWithAntPlus() {
        if (!AntPlusUtils.isAntPlusAvailable(this.applicationContext)) {
            return false;
        }
        if (this.antPlusHandler == null) {
            HandlerThread handlerThread = new HandlerThread("ant-scan");
            handlerThread.start();
            this.antPlusHandler = new Handler(handlerThread.getLooper());
        }
        if (this.antPlusScanner != null) {
            return true;
        }
        EnumSet enumSet = null;
        if (this.antPlusDeviceCallback == null) {
            this.antPlusDeviceCallback = new ANTPlusScanAdapter();
        }
        int i = this.deviceType;
        if (i == -1) {
            enumSet = EnumSet.of(DeviceType.BIKE_CADENCE, DeviceType.HEARTRATE);
        } else if (i == 2) {
            enumSet = EnumSet.of(DeviceType.BIKE_CADENCE);
        } else if (i == 3) {
            enumSet = EnumSet.of(DeviceType.HEARTRATE);
        }
        if (enumSet != null) {
            EnumSet finalEnumSet = enumSet;
            this.antPlusHandler.post(new Runnable() {
                @Override
                public void run() {
                    if (AntPlusScanner.this.antPlusDeviceCallback == null || AntPlusScanner.this.applicationContext == null) {
                        return;
                    }
                    AntPlusScanner.this.antPlusScanner = new MultiDeviceSearch(AntPlusScanner.this.applicationContext, finalEnumSet, AntPlusScanner.this.antPlusDeviceCallback);
                }
            });
        }
        return enumSet != null;
    }

    private class ANTPlusScanAdapter implements MultiDeviceSearch.SearchCallbacks {
        private ANTPlusScanAdapter() {}

        public void onSearchStarted(MultiDeviceSearch.RssiSupport param1RssiSupport) {}

        @Override
        public void onDeviceFound(com.dsi.ant.plugins.antplus.pccbase.MultiDeviceSearch.MultiDeviceSearchResult multiDeviceSearchResult) {
            int i = multiDeviceSearchResult.getAntDeviceNumber();
            if (AntPlusScanner.this.shouldNotify(String.valueOf(i))) {
                if (AntPlusScanner.this.deviceProvider == null)
                    return;
                i = AntPlusUtils.getDeviceTypeForAntPlus(multiDeviceSearchResult.getAntDeviceType());
                SmartDevice smartDevice = AntPlusScanner.this.deviceProvider.create();
                smartDevice.setType(i);
                smartDevice.setName(multiDeviceSearchResult.getDeviceDisplayName());
                smartDevice.setAddress(String.valueOf(multiDeviceSearchResult.getAntDeviceNumber()));
                smartDevice.setProtocol(2);
                AntPlusScanner.this.notifyScannedDevice(smartDevice);
            }
        }

        public void onSearchStopped(RequestAccessResult param1RequestAccessResult) {}
    }

    @Override
    public void release() {
        stopScan();
        this.antPlusScanner = null;
        this.antPlusDeviceCallback = null;
        Handler handler = this.antPlusHandler;
        if (handler != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    Looper.myLooper().quit();
                }
            });
            this.antPlusHandler = null;
        }
        super.release();
    }
}
